package com.yoyo.yyadsdk.demo;

import com.anye.greendao.gen.AdvScreenEntityDao;
import com.anye.greendao.gen.AppEntityDao;
import com.anye.greendao.gen.ConfigEntityDao;
import com.anye.greendao.gen.LocalAdvEntityDao;
import com.anye.greendao.gen.LocalAlarmEntityDao;
import com.anye.greendao.gen.LocalAppEntityDao;
import com.anye.greendao.gen.LocalPullEntityDao;
import com.anye.greendao.gen.LocalShortcutEntityDao;
import com.anye.greendao.gen.MonitorTimeEntityDao;
import com.anye.greendao.gen.PullEntityDao;
import com.anye.greendao.gen.PushEntityDao;
import com.anye.greendao.gen.ShortcutEntityDao;
import com.yoyo.yyadsdk.bean.dao.AppEntity;
import com.yoyo.yyadsdk.bean.dao.ConfigEntity;
import com.yoyo.yyadsdk.bean.dao.LocalShortcutEntity;
import com.yoyo.yyadsdk.bean.dao.MonitorTimeEntity;
import com.yoyo.yyadsdk.bean.dao.PullEntity;
import com.yoyo.yyadsdk.bean.dao.RuleListEntity;
import com.yoyo.yyadsdk.util.GreenDaoManager;

import java.util.List;

public class DBManager {

    public static List<AppEntity> getAppEntity() {
        return GreenDaoManager.getInstance().getDaoSession().getAppEntityDao().loadAll();
    }

    public static List<PullEntity> getPullEntity() {
        return GreenDaoManager.getInstance().getDaoSession().getPullEntityDao().loadAll();
    }

    public static LocalShortcutEntity getLocalShortcutEntity(int shortcutId) {
        return GreenDaoManager.getInstance().getDaoSession().getLocalShortcutEntityDao().queryBuilder()
                .where(LocalShortcutEntityDao.Properties.ShortcutId.eq(shortcutId)).unique();
    }

    public static ConfigEntity getConfigEntity() {
        List<ConfigEntity> configEntities = GreenDaoManager.getInstance().getDaoSession().getConfigEntityDao().loadAll();
        return configEntities != null && configEntities.size() > 0 ? configEntities.get(0) : null;
    }

    public static List<MonitorTimeEntity> getMonitorTime() {
        return GreenDaoManager.getInstance().getDaoSession().getMonitorTimeEntityDao().loadAll();
    }

    public static void saveAppEntity(AppEntity appEntity) {
        GreenDaoManager.getInstance().getDaoSession().getAppEntityDao().insertOrReplace(appEntity);
    }

    public static void saveLocalShortcutEntity(LocalShortcutEntity localShortcutEntity) {
        GreenDaoManager.getInstance().getDaoSession().getLocalShortcutEntityDao().insertOrReplace(localShortcutEntity);
    }

    public static void savePullEntity(PullEntity pullEntity) {
        GreenDaoManager.getInstance().getDaoSession().getPullEntityDao().insertOrReplace(pullEntity);
    }

    public static void saveConfigEntity(ConfigEntity configEntity) {
        GreenDaoManager.getInstance().getDaoSession().getConfigEntityDao().insertOrReplace(configEntity);
    }

    public static void saveMonitorTime(MonitorTimeEntity monitorTime) {
        GreenDaoManager.getInstance().getDaoSession().getMonitorTimeEntityDao().insertOrReplace(monitorTime);
    }

    public static void deleteAppEntity(AppEntity appEntity) {
        GreenDaoManager.getInstance().getDaoSession().getAppEntityDao().delete(appEntity);
    }

    public static void deletePullEntity(PullEntity pullEntity) {
        GreenDaoManager.getInstance().getDaoSession().getPullEntityDao().delete(pullEntity);
    }

    public static void deleteMonitorTime(MonitorTimeEntity monitorTime) {
        GreenDaoManager.getInstance().getDaoSession().getMonitorTimeEntityDao().delete(monitorTime);
    }

    public static void clear() {
        GreenDaoManager.getInstance().getDaoSession().deleteAll(AdvScreenEntityDao.class);
        GreenDaoManager.getInstance().getDaoSession().deleteAll(AppEntityDao.class);
        GreenDaoManager.getInstance().getDaoSession().deleteAll(ConfigEntityDao.class);
        GreenDaoManager.getInstance().getDaoSession().deleteAll(LocalAdvEntityDao.class);
        GreenDaoManager.getInstance().getDaoSession().deleteAll(LocalAlarmEntityDao.class);
        GreenDaoManager.getInstance().getDaoSession().deleteAll(LocalAppEntityDao.class);
        GreenDaoManager.getInstance().getDaoSession().deleteAll(LocalPullEntityDao.class);
        GreenDaoManager.getInstance().getDaoSession().deleteAll(LocalShortcutEntityDao.class);
        GreenDaoManager.getInstance().getDaoSession().deleteAll(MonitorTimeEntityDao.class);
        GreenDaoManager.getInstance().getDaoSession().deleteAll(PullEntityDao.class);
        GreenDaoManager.getInstance().getDaoSession().deleteAll(PushEntityDao.class);
        GreenDaoManager.getInstance().getDaoSession().deleteAll(RuleListEntity.class);
        GreenDaoManager.getInstance().getDaoSession().deleteAll(ShortcutEntityDao.class);
    }
}
